---
title: "Static and dynamic estimators"
output:
  html_document:
    toc: true
    toc_float: true
    toc_depth: 3
    code_folding: show
---

# Introduction

Please change the path on line 47 into your personal folder where the code files were placed (line47). Make sure all files from the repository are present.

This file applies the likelihood estimator (in `feol-fedoc-likelihoods.R`) to an arbitrary country.

```{r, message = FALSE, warning=FALSE}
library(cli)
library(skimr)
library(tidyverse)
library(fastDummies)
library(pracma)
library(furrr) 
library(openxlsx)
plan(multiprocess) # Set up for parallelization
set.seed('12345')
bs_reps <- 499 # Number of bootstrap replications
```

Load the auxiliary functions.

```{r}
source("feol-fedoc-likelihoods.R")
source("auxiliary-MRV.R")
source("fedol-func-emp.R")
```

Load some data and inspect it.

```{r}
# Load the data.
#Please change this path into your personal folder where the code files were placed 
load("balanced-data.RData")
df_bal <- df_bal %>% 
  mutate(
    urbanisation = as.factor(urbanisation),
    ageg = as.factor(ageg),
    educ6 = as.factor(educ6)
  )
df_country <- prep_country_data("ALL")
```

## Data definitions

Extract as vectors.

```{r}
y0 <- df_country %>% filter(period==1) %>% pull(health)
y1 <- df_country %>% filter(period==2) %>% pull(health)
y2 <- df_country %>% filter(period==3) %>% pull(health)
y3 <- df_country %>% filter(period==4) %>% pull(health)

id <- df_country %>% filter(period==4) %>% pull(i)

# Fetch  the regressors 
df_country_X <- df_country %>% 
  select(c(period,ltdhi,child,married,emp)) %>% 
  dummy_cols(remove_first_dummy = TRUE, 
             remove_selected_columns = TRUE)

X1 <- as.matrix(df_country_X %>% filter(period==2) %>% select(-period))
X2 <- as.matrix(df_country_X %>% filter(period==3) %>% select(-period))
X3 <- as.matrix(df_country_X %>% filter(period==4) %>% select(-period))

df_country_cs_X <- df_country %>% 
  select(c(period,ltdhi,child,married,emp,urbanisation,male,ageg,educ6)) %>% 
  dummy_cols(remove_first_dummy = TRUE, 
             remove_selected_columns = TRUE)

X1_wide <- as.matrix(df_country_cs_X %>% filter(period==2) %>% select(-period))
X2_wide <- as.matrix(df_country_cs_X %>% filter(period==3) %>% select(-period))
X3_wide <- as.matrix(df_country_cs_X %>% filter(period==4) %>% select(-period))

## Extract dimensions
K <- ncol(X1)
n <- nrow(X1)
J <- length(unique(c(y0,y1,y2,y3)))
k <- 3 #cutoff for RHS variable
```

## Stayers

To document "stayers", i.e. those with $X2_wide \approx X3_wide$, we condition on all columns except for the first one to be identical. The final 5 lines of the following code chunk print some summary statistics.

```{r}
discrete_difference = apply(X = X2_wide[,-1] - X3_wide[,-1], MARGIN = 1, FUN = sum)
disc_cond = which(discrete_difference == 0)
d_income_stayers = X2_wide[disc_cond,1] - X3_wide[disc_cond,1]
sum(discrete_difference == 0)
summary(d_income_stayers)
sum(abs(d_income_stayers)<1)
sum(abs(d_income_stayers)<0.1)
sum(abs(d_income_stayers)<0.01)
hist(d_income_stayers)
```

## Bootstrap sample

Draw `bs_reps` bootstrap samples and run the estimators on them. Process results and write them to `all_res-pnorm.xlsx`.

```{r}
bs_result <- 0:bs_reps %>% future_map_dfr(big_func3, .progress = TRUE)
results <- bs_result %>% pivot_longer(b_ltdhi:b_educ6_5)

results_point <- results %>% filter(s==0) %>%
  select(-c(s,point)) %>% rename(point = value)

results_bs <- results %>% filter(s>0) %>%
  group_by(estimator,h,name) %>%
  summarize(bs_mean = mean(value), bs_sd = sd(value))

results_table <- full_join(results_point,results_bs)

results_table %>% write.xlsx("all_res-pnorm.xlsx")
```
